package com.niiwoo.civet.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.mobile.vo.request.user.UserReportVO;
import com.niiwoo.civet.user.dto.request.UserReportCircle;
import com.niiwoo.civet.user.dto.request.UserReportCreateRequestDTO;
import com.niiwoo.civet.user.enums.UserReportSourceEnum;
import com.niiwoo.civet.user.enums.UserReportTypeEnum;
import com.niiwoo.civet.user.service.UserReportDubboService;
import com.niiwoo.tripod.base.utils.DateUtils;
import com.niiwoo.tripod.provider.exception.BizException;
import com.niiwoo.tripod.web.util.PrincipalContext;
import com.niiwoo.tripod.web.vo.Empty;
import com.niiwoo.tripod.web.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

import static com.niiwoo.civet.user.enums.UserReportSourceEnum.*;

/**
 * 用户举报Controller
 * @author zzl
 * @date 2018/1/19 17:20
 */
@RestController
@RequestMapping("/report")
@Api(tags = "用户举报接口(张子龙)", description = "/report")
@Slf4j
public class UserReportController {

    @Reference(version = "1.0.0")
    private UserReportDubboService userReportDubboService;

    @PostMapping("/reporting")
    @ApiOperation("用户举报")
    public Result<Void> reporting(@RequestBody UserReportVO userReportVO) {
        log.info("reportingUserReportVO接收到请求:" + userReportVO);
        String userId = PrincipalContext.getUserId();
        valid(userReportVO);
        UserReportCreateRequestDTO userReportCreateRequestDTO = cover(userReportVO);
        if(userReportCreateRequestDTO == null){
            throw new BizException("USR19087");
        }
        userReportCreateRequestDTO.setUserId(userId);
        userReportDubboService.createUserReport(userReportCreateRequestDTO);
        return Result.with(null);
    }

    private UserReportCreateRequestDTO cover(UserReportVO userReportVO){
        if(userReportVO == null){
            return null;
        }
        UserReportCreateRequestDTO userReportCreateRequestDTO = new UserReportCreateRequestDTO();
        userReportCreateRequestDTO.setReportReason(userReportVO.getRemark());
        userReportCreateRequestDTO.setReportGroupId(userReportVO.getBe_reported_group_id());
        userReportCreateRequestDTO.setReportImgUrl(userReportVO.getPicture_evidence());
        userReportCreateRequestDTO.setReportSource(userReportVO.getReport_source().byteValue());
        userReportCreateRequestDTO.setReportType(userReportVO.getReport_type().byteValue());

        userReportCreateRequestDTO.setReportContent(userReportVO.getMsg_ids());
        userReportCreateRequestDTO.setReportImid(userReportVO.getBe_reported_imid());
        if(userReportVO.getCircle() != null){
            UserReportCircle circle = new UserReportCircle();
            circle.setContent(userReportVO.getCircle().getContent());
            circle.setId(userReportVO.getCircle().getMsgid());
            circle.setImgUrls(userReportVO.getCircle().getImgUrls());
            //IM的发布时间是时间戳
            String time = DateUtils.formatDate(new Date(userReportVO.getCircle().getTime()*1000L),"yyyy-MM-dd HH:mm:ss");
            circle.setTime(time);
            userReportCreateRequestDTO.setCircle(circle);
        }

        return userReportCreateRequestDTO;
    }

    /**
     * 验证数据
     * @param userReportVO
     */
    private void valid(UserReportVO userReportVO){
        if(userReportVO == null){
            throw new BizException("USR19087");
        }
        if(userReportVO.getReport_source() == null){
            throw new BizException("USR19088");
        }
        if(enumOf(userReportVO.getReport_source().byteValue()) == null){
            throw new BizException("USR19088");
        }
        if(userReportVO.getReport_type() == null){
            throw new BizException("USR19089");
        }
        if(UserReportTypeEnum.enumOf(userReportVO.getReport_type().byteValue())==null){
            throw new BizException("USR19089");
        }
        UserReportSourceEnum sourceEnum = enumOf(userReportVO.getReport_source().byteValue());
        if(sourceEnum == UserReportSourceEnum.SOURCE_1 || sourceEnum == UserReportSourceEnum.SOURCE_5 ){
            //举报个人判断是否有用户imid
            if(StringUtils.isBlank(userReportVO.getBe_reported_imid())){
                throw new BizException("USR19090");
            }
        }else if(sourceEnum == UserReportSourceEnum.SOURCE_2 || sourceEnum == UserReportSourceEnum.SOURCE_3){
            //群举报判断是否有群id
            if(StringUtils.isBlank(userReportVO.getBe_reported_group_id())){
                throw new BizException("USR19091");
            }
        }else if(sourceEnum == UserReportSourceEnum.SOURCE_4){
            //你我圈举报
            if(StringUtils.isBlank(userReportVO.getBe_reported_imid())){
                throw new BizException("USR19090");
            }
            if(userReportVO.getCircle() == null){
                throw new BizException("USR19092");
            }
            if(userReportVO.getCircle().getMsgid() == null){
                throw new BizException("USR19092");
            }
            //是否存在你我圈图片
            boolean hasImgs = userReportVO.getCircle().getImgUrls() != null
                    && userReportVO.getCircle().getImgUrls().size() > 0;
            boolean hasContent = StringUtils.isNotBlank(userReportVO.getCircle().getContent());
            if(!hasContent && !hasImgs){
                throw new BizException("USR19092");
            }
        }


        if(sourceEnum == UserReportSourceEnum.SOURCE_1 ||sourceEnum == UserReportSourceEnum.SOURCE_2
                || sourceEnum == UserReportSourceEnum.SOURCE_3 || sourceEnum == UserReportSourceEnum.SOURCE_5){
            boolean hasImgs = userReportVO.getMsg_ids()!=null && userReportVO.getMsg_ids().size() > 0;
            boolean hasChats = userReportVO.getPicture_evidence() != null && userReportVO.getPicture_evidence().size()>0;
            //图片和聊天记录都没有
            if(!(hasImgs || hasChats)){
                throw new BizException("USR19093");
            }
        }
    }
}
